package com.ego.e.utils

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken

/**
 * Created by lxy on 2017/5/28.
 */
object EGson {
    @JvmField val gson:Gson = GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()

    @JvmStatic fun <Data> data(json: String,clazz: Class<Data>):Data{
        return gson.fromJson(json,clazz)
    }

    inline @JvmStatic fun <reified Data> data(json: String):Data{
        return gson.fromJson(json,Data::class.java)
    }

    inline @JvmStatic fun <reified Data> list(json: String):List<Data>{
        val type = object : TypeToken<List<Data>>() {}.type
        return gson.fromJson<List<Data>>(json, type)
    }

    inline @JvmStatic fun <reified Data> mutable(json: String): MutableList<Data> {
        val type = object : TypeToken<List<Data>>() {}.type
        return gson.fromJson<MutableList<Data>>(json, type)
    }

    inline @JvmStatic fun <reified Data> array(json: String):ArrayList<Data>{
        val type = object : TypeToken<List<Data>>() {}.type
        return gson.fromJson<ArrayList<Data>>(json, type)
    }

    @JvmStatic fun <Data> to(obj: Data): String {
        return gson.toJson(obj)
    }
}